<?php

/**
 * 短链
 */
class ApiUriAction extends ApiBasic
{
    //auth_key 这里不用写，但是后台添加权限需要，写这里方便后台新增权限
    protected $cmdCollect = array(
        20001 => array('method' => 'index', 'login' => true, 'auth' => true, 'auth_key' => "index"),
        20002 => array('method' => 'add', 'login' => true, 'auth' => true, 'auth_key' => "add"),
        20003 => array('method' => 'edit', 'login' => true, 'auth' => true, 'auth_key' => "edit"),
        20004 => array('method' => 'del', 'login' => true, 'auth' => true, 'auth_key' => "del"),
    );

    protected function index()
    {
        $platform_ids = $this->_cget("platform_ids");//平台id,多个逗号分隔
        if (empty($platform_ids)) {
            return $this->error('请选择平台');
        }


//        return $this->show($data);
    }

    public function add()
    {
        set_time_limit(300);
        ini_set("memory_limit", "1024M");

        $platform_ids = $this->_cget("platform_ids");//平台id,多个逗号分隔
        $dateStart = $this->_cget("datestart");
        $dateEnd = $this->_cget("dateend");
        $type = intval($this->_cget("type"));
        $type = $type == 0 ? 2 : $type; //1按发行,2按游戏,3按渠道

        //按渠道只能单选联运
        if ($type == 3 && $platform_ids != 99) {
            return $this->error('按渠道只能单选联运');
        }

        $dayStart = date('Ymd', strtotime($dateStart));
        $dayEnd = date('Ymd', strtotime($dateEnd));

        if (empty($dateStart) || empty($dayEnd)) {
            return $this->error('请选择日期');
        }
        if (empty($platform_ids)) {
            return $this->error('请选择平台');
        }

        if ($dayStart > $dayEnd) {
            $tmp = $dayEnd;
            $dayEnd = $dayStart;
            $dayStart = $tmp;
        }

        $platform_ids = explode(',', $platform_ids);
        foreach ($platform_ids as $k => $platform_id) {
            $platform_name = PlatformDefine::api_platform_list($platform_id);
            if (empty($platform_name)) {
                return $this->error("平台id错误:{$platform_id}");
            }
        }
        if ($type == 1) {
            //按CP
            $data = $this->getOrderDataCp($platform_ids, $dayStart, $dayEnd);
        } elseif ($type == 3) {
            //按渠道
            $data = $this->getOrderDataChannel($platform_ids, $dayStart, $dayEnd);
        } else {
            //按游戏
            $data = $this->getOrderDataApp($platform_ids, $dayStart, $dayEnd);
        }

        $result = array(
            'list' => $data,
            'total' => 0,
        );
        return $this->show($result);
    }

    public function edit()
    {
        set_time_limit(300);
        ini_set("memory_limit", "1024M");

        $platform_ids = $this->_cget("platform_ids");//平台id,多个逗号分隔
        $dateStart = $this->_cget("datestart");
        $dateEnd = $this->_cget("dateend");
        $type = intval($this->_cget("type"));
        $type = $type == 0 ? 2 : $type; //1按发行,2按游戏,3按渠道

        //按渠道只能单选联运
        if ($type == 3 && $platform_ids != 99) {
            return $this->error('按渠道只能单选联运');
        }

        $dayStart = date('Ymd', strtotime($dateStart));
        $dayEnd = date('Ymd', strtotime($dateEnd));

        if (empty($dateStart) || empty($dayEnd)) {
            return $this->error('请选择日期');
        }
        if (empty($platform_ids)) {
            return $this->error('请选择平台');
        }

        if ($dayStart > $dayEnd) {
            $tmp = $dayEnd;
            $dayEnd = $dayStart;
            $dayStart = $tmp;
        }

        $platform_ids = explode(',', $platform_ids);
        foreach ($platform_ids as $k => $platform_id) {
            $platform_name = PlatformDefine::api_platform_list($platform_id);
            if (empty($platform_name)) {
                return $this->error("平台id错误:{$platform_id}");
            }
        }
        if ($type == 1) {
            //按CP
            $data = $this->getOrderDataCp($platform_ids, $dayStart, $dayEnd);
        } elseif ($type == 3) {
            //按渠道
            $data = $this->getOrderDataChannel($platform_ids, $dayStart, $dayEnd);
        } else {
            //按游戏
            $data = $this->getOrderDataApp($platform_ids, $dayStart, $dayEnd);
        }

        $result = array(
            'list' => $data,
            'total' => 0,
        );
        return $this->show($result);
    }

    public function del()
    {
        set_time_limit(300);
        ini_set("memory_limit", "1024M");

        $platform_ids = $this->_cget("platform_ids");//平台id,多个逗号分隔
        $dateStart = $this->_cget("datestart");
        $dateEnd = $this->_cget("dateend");
        $type = intval($this->_cget("type"));
        $type = $type == 0 ? 2 : $type; //1按发行,2按游戏,3按渠道

        //按渠道只能单选联运
        if ($type == 3 && $platform_ids != 99) {
            return $this->error('按渠道只能单选联运');
        }

        $dayStart = date('Ymd', strtotime($dateStart));
        $dayEnd = date('Ymd', strtotime($dateEnd));

        if (empty($dateStart) || empty($dayEnd)) {
            return $this->error('请选择日期');
        }
        if (empty($platform_ids)) {
            return $this->error('请选择平台');
        }

        if ($dayStart > $dayEnd) {
            $tmp = $dayEnd;
            $dayEnd = $dayStart;
            $dayStart = $tmp;
        }

        $platform_ids = explode(',', $platform_ids);
        foreach ($platform_ids as $k => $platform_id) {
            $platform_name = PlatformDefine::api_platform_list($platform_id);
            if (empty($platform_name)) {
                return $this->error("平台id错误:{$platform_id}");
            }
        }
        if ($type == 1) {
            //按CP
            $data = $this->getOrderDataCp($platform_ids, $dayStart, $dayEnd);
        } elseif ($type == 3) {
            //按渠道
            $data = $this->getOrderDataChannel($platform_ids, $dayStart, $dayEnd);
        } else {
            //按游戏
            $data = $this->getOrderDataApp($platform_ids, $dayStart, $dayEnd);
        }

        $result = array(
            'list' => $data,
            'total' => 0,
        );
        return $this->show($result);
    }
}
